package com.guaju.yueshangLife.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.mine.OrderDetailActivity;
import com.guaju.yueshangLife.adapter.OrderDetailInnerAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.bean.Offline;
import com.guaju.yueshangLife.bean.OrderDetailBean;
import com.guaju.yueshangLife.databinding.ActivityOrderPaySuccessOfflineBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class OrderPaySuccessOfflineActivity extends BaseActivity {
    public static final String TOTAL_AMOUNT = "total_amount";
    public static final String ORDER_NUMBER = "order_number";
    public static final String TOTAL_FEE = "total_fee";

    public static final String ORDER_ID = "order_id";


    private ActivityOrderPaySuccessOfflineBinding binding;
    private int orderId;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_pay_success_offline);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {
        Intent intent = getIntent();
        int totalAmount = intent.getIntExtra(TOTAL_AMOUNT, 0);
        String totalFee = intent.getStringExtra(TOTAL_FEE);
        String orderNumber = intent.getStringExtra(ORDER_NUMBER);
        orderId = intent.getIntExtra(ORDER_ID, 0);
        binding.tvPayTotalAmount.setText("¥ " + totalFee);
        binding.btGoOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderPaySuccessOfflineActivity.this, OrderDetailActivity.class);
                intent.putExtra("order_id", orderId);
                startActivity(intent);
            }
        });
        binding.tvPaysuccessMethod.setText("线下转账");
        binding.tvSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (!TextUtils.isEmpty(orderNumber)) {
            binding.tvPayProofNum.setText(orderNumber);
        } else {
            binding.llProof.setVisibility(View.GONE);
        }

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
//        ApiEntity.orderDetail(orderId);
        ApiEntity.offline();
    }

    @Override
    protected void setListener() {

    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onOrderDetailReceived(OrderDetailBean bean) {
//        if (bean == null) {
//            showEmpty();
//        } else {
//            OrderDetailBean.DataBean data = bean.getData();
//            OrderDetailBean.DataBean.AddressBean address = data.getAddress();
//            String created_at = data.getCreated_at();
//            OrderDetailBean.DataBean.CustomerBean customer = data.getCustomer();
//            OrderDetailBean.DataBean.ExtraBean extra = data.getExtra();
//            OrderDetailBean.DataBean.ExtraBean.OfflineBean offline = extra.getOffline();
//            int id = data.getId();
//            List<OrderDetailBean.DataBean.ItemsBean> items = data.getItems();
//            String paid_at = data.getPaid_at();
//            int payment_method = data.getPayment_method();
//            String refund_status = data.getRefund_status();
//            Object remark = data.getRemark();
//            OrderDetailBean.DataBean.ShipDataBean ship_data = data.getShip_data();
//            String status = data.getStatus();
//            String total_account = data.getTotal_account();
//            int total_amount = data.getTotal_amount();
//
//
//            binding.tvPayTotalAmount.setText("¥ " + total_account);
//            String tel = address.getContact_phone();
//            String showTel = "";
//            String infoPre = "付款凭证已发送至手机";
//            String infoLast = "，该凭证需填写至【汇款用途】、【附言】、【摘要】或所有可填写备注的栏目内。";
//            String fullInfo = "";
//            if ((!TextUtils.isEmpty(tel)) && tel.length() == 11) {
//                showTel = tel.substring(0, 3) + "****" + tel.substring(7, 11);
//                fullInfo = infoPre + showTel + infoLast;
//            } else {
//                fullInfo = "付款凭证已发送至您的手机，该凭证需填写至【汇款用途】、【附言】、【摘要】或所有可填写备注的栏目内。";
//            }
//            binding.info.setText(fullInfo);
//            if (offline != null) {
//                String pay_no = offline.getPay_no();
//                String pay_bank = offline.getPay_bank();
//                String pay_name = offline.getPay_name();
//                String pay_account = offline.getPay_account();
//                binding.tvPaynumber.setText(pay_no);
//                binding.tvPayname.setText(pay_name);
//                binding.tvPaybank.setText(pay_bank);
//                binding.tvPayacount.setText(pay_account);
//            } else {
//                binding.tvPaynumber.setText("未设置");
//                binding.tvPayname.setText("未设置");
//                binding.tvPaybank.setText("未设置");
//                binding.tvPayacount.setText("未设置");
//            }
//        }
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOffline(Offline bean) {
        Offline.DataBean offline = bean.getData();
        if (offline != null) {
            String pay_account = offline.getAccount_no();
            String pay_bank = offline.getAccount_bank();
            String pay_name = offline.getAccount_name();
            String pay_no = offline.getAccount_bank_no();
            binding.tvPaynumber.setText(pay_no);
            binding.tvPayname.setText(pay_name);
            binding.tvPaybank.setText(pay_bank);
            binding.tvPayacount.setText(pay_account);
        } else {
            binding.tvPaynumber.setText("未设置");
            binding.tvPayname.setText("未设置");
            binding.tvPaybank.setText("未设置");
            binding.tvPayacount.setText("未设置");
        }
        }
    }
